﻿using System.Collections.Generic;

namespace Douyu.SDK
{
    /// <summary>
    /// 消息基础类
    /// </summary>
    /// <remarks>
    /// RFC格式:
    /// +--------+--------+--------+--------+
    /// |12345678|12345678|12345678|12345678|
    /// +--------+--------+--------+--------+
    /// |     Len(消息体长度 + 8)           |
    /// +--------+--------+--------+--------+
    /// |          Len2(校验长度)           |
    /// +--------+--------+--------+--------+
    /// | MsgType:消息类型|   EK   |   Tag  |
    /// +--------+--------+--------+--------+
    /// |  MessageBody: 消息体(以'\0'结束)  |
    /// +--------+--------+--------+--------+
    /// 
    /// 坑1: 该格式缺少协议版本标识, 
    ///     无法做到新旧两个协议版本并存, 
    ///     无法提供给第三方客户端修改升级过渡的时间.
    ///     可能导致协议升级时出现大量第三方客户端无法使用.
    ///     同时也影响自身客户端的发布方式.
    /// 坑2: Len = 消息体长度 + 8 + 1;(怎么算出来的8)
    /// 坑3: 缺少头部长度的字段, 以后要扩展头部信息, 会有阻碍.
    /// </remarks>
    public class BaseMessage : SafeDictionary
    {
        public BaseMessage()
        {
        }

        protected BaseMessage(BaseMessage msg) : base(msg.Datas)
        {
            this.Len = msg.Len;
            this.Len2 = msg.Len2;
            this.EncodeKey = msg.EncodeKey;
            this.MsgType = msg.MsgType;
            this.Tag = msg.Tag;
            this.MessageBody = msg.MessageBody;
        }

        /// <summary>
        /// 构造函数(发送时专用)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="msgType"></param>
        public BaseMessage(string type, MsgType msgType)
        {
            this.type = type;
            this.MsgType = msgType;
        }

        /// <summary>
        /// 包总长度
        /// </summary>
        public int Len { get; set; } = 0;

        /// <summary>
        /// 校验长度
        /// </summary>
        public int Len2 { get; set; } = 0;

        /// <summary>
        /// 消息类型(发送方向)
        ///     689 客户端 -> 服务器
        ///     690 服务器 -> 客户端
        /// </summary>
        public MsgType MsgType { get; set; }

        /// <summary>
        /// 加密字段(EK)
        /// </summary>
        public byte EncodeKey { get; set; } = 0;

        /// <summary>
        /// 保留字段
        /// </summary>
        public byte Tag { get; set; } = 0;

        /// <summary>
        /// 消息主体(接收专用)
        /// </summary>
        public string MessageBody { get; set; } = string.Empty;

        public void setDatas(Dictionary<string, object> datas)
        {
            this.Datas = datas;
        }

    }

}
